﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using Microsoft.Reporting.WebForms;
using Viewer.Base.Config;
using System.Collections.Generic;
using Viewer.NhanSu.Reporting;

namespace Viewer.NhanSu.Reporting
{
    public partial class RptForm : System.Web.UI.Page
    {
        protected void MasterScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            // TODO: see if this is ever invoked
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            var MasterScriptManager = (ScriptManager)this.FindControl("MasterScriptManager");
            MasterScriptManager.EnablePartialRendering = false;
            MasterScriptManager.RegisterPostBackControl((Control)ReportViewerMain);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterScriptManager.RegisterPostBackControl((Control)ReportViewerMain);

            System.Globalization.CultureInfo elGR = new System.Globalization.CultureInfo("tr-TR");
            System.Threading.Thread.CurrentThread.CurrentUICulture = elGR;

            LoadReport();
        }


        private void LoadReport()
        {
            string strpara = Request.QueryString["para"];
            string[] strparameters = strpara.Split(',');
            string strReportPath = string.Empty;
            ReportViewerMain.Visible = true;
            ReportViewerMain.Reset();

            ReportViewerMain.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            // Configuration setting from Reports.config
            // Path for Server Report
            ReportViewerMain.ServerReport.ReportServerUrl = new Uri(ViewerConstant.ReportUrl);

            strReportPath = string.Format("/{0}/{1}", "ReportService", ReturnReportPath(strparameters[0]));

            ReportViewerMain.ServerReport.ReportPath = strReportPath;

            List<ReportParameter> parameters = new List<ReportParameter>();

            if (strparameters[0].Equals(ViewerConstant.NameReport.NSThayDoiSoLuongNhanVien))
            {
                parameters.Add(new ReportParameter("ToChucID", strparameters[1]));
                parameters.Add(new ReportParameter("Loai", strparameters[2]));
                parameters.Add(new ReportParameter("FromMonth", strparameters[3]));
                parameters.Add(new ReportParameter("EndMonth", strparameters[4]));
                parameters.Add(new ReportParameter("FromYear", strparameters[5]));
                parameters.Add(new ReportParameter("EndYear", strparameters[6]));
            }
            else if (strparameters[0].Equals(ViewerConstant.NameReport.NSBaoCaoNgach))
            {
                parameters.Add(new ReportParameter("ToChucID", strparameters[1]));
                parameters.Add(new ReportParameter("NgachID", strparameters[2]));
                parameters.Add(new ReportParameter("TenNgach", strparameters[3]));
                parameters.Add(new ReportParameter("FromYear", strparameters[4]));
                parameters.Add(new ReportParameter("EndYear", strparameters[5]));
            }
            else if (strparameters[0].Equals(ViewerConstant.NameReport.NSBaoCaoNghe))
            {
                parameters.Add(new ReportParameter("ToChucID", strparameters[1]));
                parameters.Add(new ReportParameter("NgheNghiepID", strparameters[2]));
                parameters.Add(new ReportParameter("TenNghe", strparameters[3]));
                parameters.Add(new ReportParameter("FromYear", strparameters[4]));
                parameters.Add(new ReportParameter("EndYear", strparameters[5]));
            }
            else
            {
                parameters.Add(new ReportParameter("TenDonVi", strparameters[1]));
                parameters.Add(new ReportParameter("ToChucID", strparameters[2]));
                parameters.Add(new ReportParameter("Nam", strparameters[3]));
                parameters.Add(new ReportParameter("TenBaoCao", strparameters[0]));
            }

            ReportViewerMain.Height = Convert.ToInt32(ViewerConstant.ReportHeight) - 40;

            ReportViewerMain.ServerReport.SetParameters(parameters);
            ReportViewerMain.ShowParameterPrompts = false;
            ReportViewerMain.ShowPromptAreaButton = false;
            ReportViewerMain.ServerReport.Refresh();
            ReportViewerMain.AsyncRendering = false;

        }

        private string ReturnReportPath(string para)
        {
            string strReportPath = string.Empty;
            if (para.Equals(ViewerConstant.NameReport.NSNgheNghiep))
            {
                strReportPath = ViewerConstant.ReportURL.RptPhanBoNgheNghiep;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSNgachCongChuc))
            {
                strReportPath = ViewerConstant.ReportURL.RptPhanBoNgachCongChuc;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSChuyenNganh))
            {
                strReportPath = ViewerConstant.ReportURL.RptChuyenNganh;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSTrinhDoChuyenMon))
            {
                strReportPath = ViewerConstant.ReportURL.RptTrinhDoChuyenMon;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSBangCap))
            {
                strReportPath = ViewerConstant.ReportURL.RptBangCap;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSLiLuanChinhTri))
            {
                strReportPath = ViewerConstant.ReportURL.RptLiLuanChinhTri;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSQuanLiNhaNuoc))
            {
                strReportPath = ViewerConstant.ReportURL.RptQuanLiNhaNuoc;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSTrinhDoNgoaiNgu))
            {
                strReportPath = ViewerConstant.ReportURL.RptNgoaiNgu;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSTinHoc))
            {
                strReportPath = ViewerConstant.ReportURL.RptTrinhDoTinHoc;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSNghiPhep))
            {
                strReportPath = ViewerConstant.ReportURL.RptNghiPhep;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSPhanBoHopDong))
            {
                strReportPath = ViewerConstant.ReportURL.RptPhanBoHopDong;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSBacLuong))
            {
                strReportPath = ViewerConstant.ReportURL.RptBacLuong;
            }

            else if (para.Equals(ViewerConstant.NameReport.NSPhuCapChucVu))
            {
                strReportPath = ViewerConstant.ReportURL.RptChucVu;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSPhuCapKhac))
            {
                strReportPath = ViewerConstant.ReportURL.RptPhuCapKhac;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSDaoTao))
            {
                strReportPath = ViewerConstant.ReportURL.RptDaoTao;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSKhenThuong))
            {
                strReportPath = ViewerConstant.ReportURL.RptKhenThuong;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSKiLuat))
            {
                strReportPath = ViewerConstant.ReportURL.RptKiLuat;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSThayDoiSoLuongNhanVien))
            {
                strReportPath = ViewerConstant.ReportURL.RptThayDoiSoLuongNhanVien;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSBaoCaoNghe))
            {
                strReportPath = ViewerConstant.ReportURL.RptBaoCaoNgheTheoNam;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSBaoCaoNgach))
            {
                strReportPath = ViewerConstant.ReportURL.RptBaoCaoNgachTheoNam;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSBaoCaoNhanVien))
            {
                strReportPath = ViewerConstant.ReportURL.RptBaoCaoNhanVien;
            }
            else if (para.Equals(ViewerConstant.NameReport.NSTiLeNamNu))
            {
                strReportPath = ViewerConstant.ReportURL.RptTiLeNamNu;
            }
            return strReportPath;
        }
    }
}
